package org.wang.generalcode.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.sys.pojo.entity.Log;
import org.wang.generalcode.modules.sys.pojo.params.LogPageParams;
import org.wang.generalcode.modules.sys.service.ILogService;
import org.wang.generalcode.modules.sys.pojo.vo.LogPageVO;
import org.wang.generalcode.modules.sys.pojo.vo.LogStatsVO;
import org.wang.generalcode.modules.sys.pojo.vo.LogVO;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;

/**
 * 系统日志表 控制器
 *
 * @author wz
 * @since 2024-08-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/log")
@Schema(description = "系统日志表", name = "系统日志表接口")
public class LogController {

    private final ILogService logService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @Schema(description = "详情", example = "传入log")
    public Result<Log> detail(Log log) {
        Log detail = logService.getOne(Condition.getQueryWrapper(log));
        return Result.success(detail);
    }

    /**
     * 分页 系统日志表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @Schema(description = "分页", example = "传入log")
    public Result<IPage<Log>> list(Log log, Query query) {
        IPage<Log> pages = logService.page(Condition.getPage(query), Condition.getQueryWrapper(log));
        return Result.success(pages);
    }

    /**
     * 自定义分页 系统日志表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @Schema(description = "分页", example = "传入log")
    public Result<IPage<LogPageVO>> page(LogPageParams logPageParams, Query query) {
        IPage<LogPageVO> pages = logService.selectLogPage(Condition.getPage(query), logPageParams);
        return Result.success(pages);
    }

    /**
     * 新增 系统日志表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @Schema(description = "新增", example = "传入log")
    public Result save(@Valid @RequestBody Log log) {
        return Result.judge(logService.save(log));
    }

    /**
     * 修改 系统日志表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @Schema(description = "修改", example = "传入log")
    public Result update(@Valid @RequestBody Log log) {
        return Result.judge(logService.updateById(log));
    }

    /**
     * 新增或修改 系统日志表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @Schema(description = "新增或修改", example = "传入log")
    public Result submit(@Valid @RequestBody Log log) {
        return Result.judge(logService.saveOrUpdate(log));
    }


    /**
     * 删除 系统日志表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @Schema(description = "删除", example = "传入ids")
    public Result remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
        return Result.judge(logService.removeByIds(Func.toLongList(ids)));
    }


    @Schema(description = "获取访问趋势")
    @GetMapping("/visit-trend")
    public Result<LogVO> getVisitTrend(
            @Parameter(name = "开始时间", example = "yyyy-MM-dd") @RequestParam String startDate,
            @Parameter(name = "结束时间", example = "yyyy-MM-dd") @RequestParam String endDate
    ) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        LogVO data = logService.getVisitTrend(start, end);
        return Result.success(data);
    }


    @Schema(description = "获取统计数据")
    @GetMapping("/visit-stats")
    public Result<List<LogStatsVO>> getVisitStats() {
        List<LogStatsVO> list = logService.getVisitStats();
        return Result.success(list);
    }

}
